﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VideoCache.Server.Model;
using VideoCache.Server.BLL;
using DbMapping.Common;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Models
{
    public class AlbumSiteModel : AlbumInfo
    {
        public List<SiteEpisodesModel> Sites { get; set; }

        public AlbumSiteModel(int albumid)
        {
            this.Sites = new List<SiteEpisodesModel>();

            AlbumInfo info = AlbumBLL.Find(albumid);

            if (info == null) throw new ArgumentException("指定的专辑编号不存在。");

            this.Id = info.Id;
            this.Title = info.Title;
            this.TypeId = info.TypeId;
            this.AreaId = info.AreaId;
            this.Director = info.Director;
            this.Actor = info.Actor;
            this.IsEnd = info.IsEnd;
            this.Priority = info.Priority;
            this.PubYear = info.PubYear;
            this.Rating = info.Rating;
            this.Status = info.Status;
            this.BaiduId = info.BaiduId;
            this.CreateTime = info.CreateTime;
            this.ModifyTime = info.ModifyTime;

            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, this.Id));

            Orders orders = new Orders();
            orders.Add(new OrderItem(EpisodeInfo.SiteIdField, OrderDirection.Asc));
            orders.Add(new OrderItem(EpisodeInfo.EpisodeIndexField, OrderDirection.Asc));

            var episodes = EpisodeBLL.FindAll(cons, orders);

            foreach (SiteInfo siteInfo in SiteBLL.GetAlbumSites(this.Id))
            {
                SiteEpisodesModel siteModel = new SiteEpisodesModel();
                siteModel.Id = siteInfo.Id;
                siteModel.Name = siteInfo.Name;
                siteModel.DisplayName = siteInfo.DisplayName;
                siteModel.Name = siteInfo.Name;
                siteModel.PlayerId = siteInfo.PlayerId;
                siteModel.PositionTag = siteInfo.PositionTag;
                siteModel.UrlHost = siteInfo.UrlHost;
                siteModel.AllowDownload = siteInfo.AllowDownload;
                siteModel.Downloader = siteInfo.Downloader;
                siteModel.CreateTime = siteInfo.CreateTime;
                siteModel.ModifyTime = siteInfo.ModifyTime;
                siteModel.Episodes.AddRange(episodes.FindAll(p => p.SiteId == siteModel.Id));

                this.Sites.Add(siteModel);
            }
        }
    }
}